package net.xdclass.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.model.AddressDO;
import net.xdclass.request.AdressAddRequest;
import net.xdclass.service.AddressService;
import net.xdclass.util.JsonData;
import net.xdclass.vo.AddressVo;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 电商-公司收发货地址表 前端控制器
 * </p>
 *
 * @author JZ
 * @since 2023-04-26
 */
@Api(tags = "收货地址模块")
@RestController
@RequestMapping("/api/address/v1")
public class AddressController {

    @Resource
    private AddressService addressService;

    @ApiOperation("新增收货地址")
    @PostMapping("/add")
    public JsonData add(@RequestBody AdressAddRequest adressAddRequest) {
        int rows = addressService.add(adressAddRequest);
        return JsonData.buildSuccess();
    }

    @ApiOperation("根据id查找地址详情")
    @GetMapping("/find/{address_id}")
    public JsonData detail(@PathVariable long address_id) {
        AddressVo detail = addressService.detail(address_id);
        return ObjectUtils.isEmpty(detail) ? JsonData.buildResult(BizCodeEnum.ADDRESS_NO_EXITS) : JsonData.buildSuccess(detail);
    }

    @ApiOperation("删除指定收货地址")
    @DeleteMapping("/del")
    public JsonData del(@RequestParam("address_id") long address_id) {
        int rows = addressService.del(address_id);
        return rows == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.ADDRESS_DEL_FAIL);
    }

    @ApiOperation("获取全部收货地址")
    @GetMapping("/list")
    public JsonData list() {
        List<AddressVo>  addressVoList = addressService.list();
        return JsonData.buildSuccess(addressVoList);
    }
}

